package njupt.daoimp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

import njupt.dao.UserDAO;
import njupt.vo.User;
import njupt.vo.pagination;

public class UserDAOimp implements UserDAO {
	private Connection conn=null;
	private PreparedStatement ps=null ;
	private ResultSet rs=null;

	public UserDAOimp(Connection conn){
		
		this.conn=conn;
		System.out.println("123");
		}
	
	public boolean checkusers(User user) throws Exception {
		boolean flag=false;
		try{
		String sql="SELECT name,type,realname,email,phone,address,zipcode FROM users WHERE id=? AND password=?";
		this.ps=this.conn.prepareStatement(sql);
		this.ps.setString(1, user.getId());
		this.ps.setString(2,user.getPassword());
		  System.out.println(user.getId());
		  System.out.println(user.getPassword());
		 rs=this.ps.executeQuery();
		 System.out.println("到了此处123");
		  if(rs.next()){
			user.setName(rs.getString(1));
			System.out.println(rs.getInt(2));
		    user.setType(rs.getInt(2));
		    user.setRealname(rs.getString(3));
		    user.setEmail(rs.getString(4));
		    user.setPhone(rs.getString(5));
		    user.setAddress(rs.getString(6));
		    user.setZipcode(rs.getString(7));
			System.out.println(user.getName());
			flag=true;
		     }
		  else{flag=false;}
		}catch(Exception e){
			throw e;
		}finally{
			if(this.conn!=null){
				try{
					this.ps.close();
				}catch(Exception e){
					throw e;
				}
			}
		}
		System.out.println("flag"+flag);
		return flag;
		
	}
	 public int insertuser(User user) throws Exception{
	     int flag=0;
		 try{
			String sql="INSERT INTO users(id,password,name) values(?,?,?)";
			System.out.println("INSERT INTO users(id,password,name) values(?,?,?)");
			this.ps=this.conn.prepareStatement(sql);
			this.ps.setString(1, user.getId());
			this.ps.setString(2,user.getPassword());
			this.ps.setString(3,user.getName());
			this.ps.executeUpdate();
			flag=1;
		 }catch(Exception e){
			 throw e ;
		 }
		 
		 return flag;
	 }
	 
	 public List<String[]> showuser(int currentpage,int pagesize) throws  Exception{
		    
		 List<String[]> usermsg = new ArrayList<String[]>();
		  /* pagination pg=new pagination();   
		   int CURRENTPAGE=pg.getCurrentpage();  
           int PAGESIZE = pg.getPagesize();*/
           System.out.println("CURRENTPAGE"+currentpage);
           System.out.println("PAGESIZE"+pagesize);
		   try{
			    String sql="select * from USERS where rownum<"+pagesize+"*"+currentpage+" minus select * from USERS where rownum <"+pagesize+"*"+(currentpage-1)+"";
			    ps=this.conn.prepareStatement(sql);
			   // System.out.println("userDaoimp.java 执行execute");
			    rs=this.ps.executeQuery();
			    System.out.println("userDaoimp.java 执行execute后");
			    ResultSetMetaData rsmd = rs.getMetaData();
			    int columncount = rsmd.getColumnCount();
			    System.out.println("userDaoimp.java 执行");
			    while(rs.next()){
			    	String[] str = new String[columncount];
			    	for(int i=0;i<columncount;i++){
			    	     str[i]=rs.getString(i+1);
			    	   }
		            usermsg.add(str);
		            System.out.println("str[0]的值为："+str[0]+"  "+"str[1]的值为:"+str[1]+"  " + "str[2]的值为:"+str[2]);
			    }
			     		  
			 
		   }catch(Exception e){
			   throw e;
		   }
		   System.out.println("userDAOimp return");
		return usermsg ;
		//System.out.println("userDAOimp return");
		 
	 }

	public int counttotalpage() throws Exception {
		pagination pg=new pagination();
		int pagesize= pg.getPagesize();
		
		   try{
			    int counts = 0 ;
			    
			    String sql="SELECT * FROM users";
			    ps=this.conn.prepareStatement(sql);
			    rs=this.ps.executeQuery();		   
			    System.out.println("userDaoimp.java 执行");
			    while(rs.next()){
			    	counts++;		      
			    }
			    
			    if(counts%pagesize==0){
			    	return counts/pagesize;
			    }
			   return (counts/pagesize+1);
			   // System.out.println("Totalpage");
		   }catch(Exception e){
			   throw e;
			   
		   }
		   
	}

	public int updateuserinfo(String realname, String phone, String address,
			String zipcode, String email,String userid) throws Exception {
		    int flag=0;
		    try{
		    	String sql="update users SET realname=?,phone=?,address=?,zipcode=?,email=? where ID=? ";
		    	ps=this.conn.prepareStatement(sql);
		    	ps.setString(1, realname);
		    	ps.setString(2, phone);
		    	ps.setString(3, address );
		    	ps.setString(4, zipcode);
		    	ps.setString(5, email);
		    	ps.setString(6, userid);
		    	this.ps.executeUpdate();
		    	flag=1;
		    }catch(Exception e){
		    	throw e;}
		return flag;
	}

	/* Overriding methods
	 */
	public int checkuserexit(int userid) throws Exception {
		String sql="select password from users where id=?";
		
		int flag=0;
		try{
			ps=this.conn.prepareStatement(sql);
			ps.setInt(1, userid);
			rs=ps.executeQuery();
          if(rs.next()){
        	  flag=1;
          }
		}catch(Exception e){throw e;}
		return flag;
	}

}
